var app = getApp()
var _function = require('../../../utils/functionData.js');
const requestUtil = require('../../../utils/requestUtil');
const _DgData = require('../../../utils/data');
const QR = require('../../../utils/qrcode');
const s_common = require('../store-common/common');

Page({
    data:{
        this_order_id:0,
        oinfo:[],
        store_data:[],
        store_id:0,
        glo_is_load:true,
        is_show_pay_type:false,
        pay_type_index:1,
       
    },
    onLoad: function (options) {
      var that = this;
      var order_id = options.oid;
      that.setData({
        this_order_id: order_id,
      })
      //请求订单详情
      that.getOrderInfo();
      that.getStoreConfig();
    },
    //获取配置信息
    getStoreConfig: function () {
      var that = this;
      requestUtil.get(_DgData.duoguan_host_api_url + '/index.php?s=/addon/DgStore/Api/getStoreConfig.html', {}, (info) => {
        that.setData({
          store_type: info.store_type,
        });
      });
    },
    //跳转店铺
    go_store_info_bind:function(e){
        var that = this;
        var store_id = e.currentTarget.id;
        wx.navigateTo({
            url: '../store-info/index?store_id='+store_id
        });
    },
    //获取订单信息
    getOrderInfo:function(){
      var that = this;
      requestUtil.get(_DgData.duoguan_host_api_url + '/index.php?s=/addon/DgStore/OrderApi/getOrderInfo.html', {oid:that.data.this_order_id}, (data) => {
        var that = this;
        that.createQrCode(data.order_sn, "mycanvas");
        that.setData({
          oinfo: data,
          glo_is_load: false
        });
        that.data.store_id=data.store_id;
        that.getStoreInfo();
      });
    },
    //获取店铺信息 
    getStoreInfo: function () {
      var that = this;
      requestUtil.get(_DgData.duoguan_host_api_url + '/index.php?s=/addon/DgStore/StoreApi/getStoreBasicInfo.html', { store_id: that.data.store_id }, (info) => {
        var store_data = info;
        that.data.store_data = store_data;
        that.setData({
          store_data: store_data,
          is_show: true
        });
      }, this, { isShowLoading: false });
    },
    //拨号
    call_phone_bind:function(){
      var that = this;
      console.log(that.data.oinfo.store_con_mobile);
      wx.makePhoneCall({
        phoneNumber: that.data.store_data.store_con_mobile
      })
    },
    //评价
    order_go_comment_bind: function (e) {
      var oid = e.currentTarget.id;
      wx.navigateTo({
        url: '../store-order-comment/index?oid=' + oid
      });
    },
    //改变订单状态
    change_order_status_or_show: function (e) {
      var that = this
      var oid = e.currentTarget.id;
      var change_type = e.currentTarget.dataset.change_type;
      var content = '';
      if (change_type == 1) {
        //取消订单
        content = '确认要取消订单吗？';
      }
      else if (change_type == 2) {
        // 确认已送达
  
        if (that.data.oinfo.order_type==1){
          content = '确认已领取吗？';
        }
        else{
          content = '确认送达吗？';
        }

      }
      else if (change_type == 3) {
        content = '确定要删除订单吗？';
      }
      wx.showModal({
        title: '提示',
        content: content,
        success: function (res) {
          if (res.confirm == true) {
            if(change_type==1||change_type==3){
              wx.navigateBack({
                delta:1
              })

            }else{
            requestUtil.get(_DgData.duoguan_host_api_url + '/index.php?s=/addon/DgStore/OrderApi/changeOrderStatusOrShow.html', { oid: oid, change_type: change_type }, (info) => {
              that.setData({
                page_index: 1
              })
              that.getOrderInfo();
            });
            }
          }
        }
      })

    },
    //开始支付
    order_go_pay_bind: function (e) {
      var that = this;
      var this_order_id = e.currentTarget.id;
      that.setData({
        buttonIsDisabled: true,
        submitIsLoading: true
      })
      that.orderPay(this_order_id);
    },
    //订单支付
    orderPay: function (oid) {
      var that = this;
      var pay_id=that.data.pay_type_index
      wx.showModal({
        title: '提示',
        content: "确定支付吗",
        success: function (res) {
          if (res.confirm) {
      requestUtil.get(_DgData.duoguan_host_api_url + '/index.php?s=/addon/DgStore/StorePayApi/order_pay.html', { oid: oid,pay_id:pay_id }, (data) => {


       if (data['type'] == 2) {
          //余额不足去充值
          wx.navigateTo({
            url: '/pages/user/mcard/recharge',
          });
        }
        else if (data['type'] == 3||data['type'] == 1) {
          //3余额支付 2卡券直接抵用
          wx.showModal({
            title: '提示',
            content: "订单支付成功",
            confirmText: "确定",
            showCancel: false,
            success: function (res) {
            }
          });
        }
        else
        {
          wx.requestPayment({
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.package,
            'signType': 'MD5',
            'paySign': data.paySign,
            'complete': function () {
            }
          });
        }
                   //支付完成 刷新
       that.getOrderInfo();
      }, { complete:that.orderPayComplete() });
          }
          else {
            that.setData({
              btn_submit_disabled: false,
              submitIsLoading: false
            });
          }

        }
      });
    },
    //支付完成
    orderPayComplete: function () {
      var that = this;
      that.setData({
        buttonIsDisabled: false,
        submitIsLoading: false
      });
    },
    //下拉刷新
    onPullDownRefresh:function(){
      var that = this;
      that.setData({
        this_page:1
      });
      that.getOrderInfo();
      setTimeout(()=>{
        wx.stopPullDownRefresh()
      },1000)
    },
    //创建二维码
    createQrCode: function (url, canvasId) {
      //调用插件中的draw方法，绘制二维码图片
      QR.qrApi.init(url,canvasId,10, 10, 120, 120);

    },
      //导航导航
  get_location_bind: function () {
      var that = this;
      var loc_lat = that.data.oinfo.store_gps_lat;
      var loc_lng = that.data.oinfo.store_gps_lng;
      wx.openLocation({
        latitude: parseFloat(loc_lat),
        longitude: parseFloat(loc_lng),
        scale: 18,
        name: that.data.oinfo.store_name,
        address: that.data.oinfo.store_address
      });
    },
  //显示支付类型选择
  show_paytype_bind: function () {
    var that = this;
    that.setData({
      is_show_pay_type: !that.data.is_show_pay_type
    });
  },
  //支付方式选择
  payTypeChange: function (e) {
    var index = e.detail.value;
    this.setData({
      pay_type_index: index,
    });
  },
})